package com.vortex.cloud.zhsw.xinyang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.vortex.cloud.zhsw.xinyang.service.RedisDemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author bending
 */
@Slf4j
@Component
public class RedisDemoServiceImpl implements RedisDemoService {

    /**
     * demo的redis Key 前缀
     * redis Key ：服务名:业务/模块名称:
     *  示例： SAMPLE:DEMO:
     */
    public final static String REDIS_KEY = "SAMPLE:DEMO:";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void valueOperations(){
        String key = REDIS_KEY+"value:";
        int count = 5;
        for (int i = 0; i < count; i++) {
            stringRedisTemplate.delete(key+i);
        }
        log.info("KV 清空数据");

        
        String value;
        for (int i = 0; i < count; i++) {
            log.info("KV 设值[key={},value={}]",key+i, i);
            stringRedisTemplate.opsForValue().set(key+i, String.valueOf(i));
        }

        //设值并设置1分钟的有效期
        stringRedisTemplate.opsForValue().set(key+"test", "有效期测试",1, TimeUnit.MINUTES);

        for (int i = 0; i < count; i++) {
            value = stringRedisTemplate.opsForValue().get(key+i);
            log.info("KV 取值[key={},value={}]",key+i, value);
        }
    }

    @Override
    public void listOperations(){
        String key = REDIS_KEY+"list";
        stringRedisTemplate.delete(key);
        log.info("list 清空数据");
        
        // 当end=-1时，取list的全部数据
        List<String> list = stringRedisTemplate.opsForList().range(key,0,-1);
        log.info("list数据：{}", JSONObject.toJSONString(list));

        // 如果key存在则插入到队首
        Long result = stringRedisTemplate.opsForList().leftPushIfPresent(key,"测试");
        list = stringRedisTemplate.opsForList().range(key,0,-1);
        log.info("如果key存在则插入到队首结果：{}，list数据：{}",result, JSONObject.toJSONString(list));

        // 如果key存在则插入到队尾
        result = stringRedisTemplate.opsForList().rightPushIfPresent(key,"测试");
        list = stringRedisTemplate.opsForList().range(key,0,-1);
        log.info("如果key存在则插入到队首，结果：{}， list数据：{}",result, JSONObject.toJSONString(list));

        int count = 3;
        // 插入到队首
        for (int i = 0; i < count; i++) {
            stringRedisTemplate.opsForList().leftPush(key,String.valueOf(i));
            log.info("list 插入队首，value={}",i);
            list = stringRedisTemplate.opsForList().range(key,0,-1);
            log.info("list数据：{}", JSONObject.toJSONString(list));
        }

        // 插入到队尾
        for (int i = 0; i < count; i++) {
            stringRedisTemplate.opsForList().rightPush(key,String.valueOf(i));
            log.info("list 插入队尾，value={}",i);
            list = stringRedisTemplate.opsForList().range(key,0,-1);
            log.info("list数据：{}", JSONObject.toJSONString(list));
        }

        // 如果key存在则插入到队首
        result = stringRedisTemplate.opsForList().leftPushIfPresent(key,"测试1");
        list = stringRedisTemplate.opsForList().range(key,0,-1);
        log.info("如果key存在则插入到队首结果：{}，list数据：{}",result, JSONObject.toJSONString(list));
        
        // 如果key存在则插入到队尾
        result = stringRedisTemplate.opsForList().rightPushIfPresent(key,"测试2");
        list = stringRedisTemplate.opsForList().range(key,0,-1);
        log.info("如果key存在则插入到队首，结果：{}， list数据：{}",result, JSONObject.toJSONString(list));
    }

    @Override
    public void setOperations() {
        String key = REDIS_KEY+"set";
        stringRedisTemplate.delete(key);
        log.info("set 清空数据");

        Set<String> list = stringRedisTemplate.opsForSet().members(key);
        log.info("set：{}", JSONObject.toJSONString(list));
        int count = 3;
        Long result;
        for (int i = 0; i < count; i++) {
            result = stringRedisTemplate.opsForSet().add(key,"测试");
            list = stringRedisTemplate.opsForSet().members(key);
            log.info("结果：{}，set数据：{}",result, JSONObject.toJSONString(list));
        }

        log.info("------------------------------------------");
        String value;
        for (int i = 0; i < count; i++) {
            value = stringRedisTemplate.opsForSet().pop(key);
            list = stringRedisTemplate.opsForSet().members(key);
            log.info("取值结果：{}，set数据：{}",value, JSONObject.toJSONString(list));
        }

    }

    @Override
    public void hashOperations() {
        String key = REDIS_KEY+"hash";
        stringRedisTemplate.delete(key);
        log.info("hash 清空数据");
        int count = 5;
        String value;
        for (int i = 0; i < count; i++) {
            log.info("hash 设值[key={},value={}]",key+i, i);
            stringRedisTemplate.opsForHash().put(key,key+i, String.valueOf(i));
        }
        log.info("------------------------------------------");
        BoundHashOperations<String,String,String> ops = stringRedisTemplate.boundHashOps(key);
        for (int i = 0; i < count; i++) {
            value = ops.get(key+i);
            log.info("hash 取值[key={},value={}]",key+i, value);
        }
    }

}
